if not defined JAVA_HOME (
  echo please set JAVA_HOME
  goto :EOF
)

:MAIN
	if {%1}=={pack} (
		shift
		goto :JAVA_pack
	)
	
    SET JAVA_LOG=%CD%\build.log
    SET JAVA_ERROR=%CD%\error.log

    if exist error.log del/s/q error.log

    echo make for %CD% > %JAVA_LOG%
    call :JAVA_MAKE 1>> %JAVA_LOG%  2>>%JAVA_ERROR%
    call :DELETE_IF_SIZE_EQ_0 %JAVA_ERROR%

goto :EOF

:JAVA_PACK
    @if exist jmake_cfg.bat (
        @call jmake_cfg.bat
    )
	
	@SET JAVA_PACK_NAME=%1
	@SET JAVA_PACK_S_NAME=%~n1
	@shift
	
	@if not exist %JAVA_PACK_NAME% @(
		@echo %JAVA_PACK_NAME% not found
		@goto :EOF
	)
	
	@if not exist .pack mkdir .pack
	@if not exist .pack\src  mkdir .pack\src
	@if not exist .pack\dest mkdir .pack\dest
	
	@pushd .pack
	@cd src
    @jar xf ..\..\%JAVA_PACK_NAME%
	@cd ..
	
	@if exist src\MANIFEST.MF @(
		@set JAVA_MANI_FILE=%CD%\src\MANIFEST.MF
	) else if exist src\META-INF\MANIFEST.MF (
		@set JAVA_MANI_FILE=%CD%\src\META-INF\MANIFEST.MF
	) else @(
		@echo MANIFEST.MF not found
		@goto :EOF
	)

	@SET JAVA_PACK_ERROR=
	@echo begin pack
	@popd
	
:EXEC_LOOP	
    @SET JAVA_CONT=
	@echo begin error >.pack\err
	@echo begin log   >.pack\log
	@java -classpath .pack\dest %1 %2 %3 %4 %5 %6 %7 %8 %9 2>.pack\err  1>.pack\log
	@type .pack\log >>.pack\err
	@type err
	@for /F "tokens=1,2 delims= " %%i in ('type .pack\err') do @(
	   @if {%%i}=={java.lang.NoClassDefFoundError:} call :JAVA_PACK_CP %%j
	)
	@if defined JAVA_CONT goto EXEC_LOOP	
	
	@for /F "tokens=5,6 delims= " %%i in ('type .pack\err') do @(
	   @if {%%i}=={java.lang.NoClassDefFoundError:} call :JAVA_PACK_CP %%j
	)
	
	@if defined JAVA_CONT goto EXEC_LOOP	
	
	@if defined JAVA_PACK_ERROR goto :EOF
	
	@cd .pack\dest
	@jar cvfm ..\..\%JAVA_PACK_S_NAME%.pack.jar %JAVA_MANI_FILE% *
	@cd ..\..
	
	@echo end pack
goto :EOF

:JAVA_PACK_CP
@SET JAVA_CONT=YES
@SET JAVA_CP_FILE=%1
@SET JAVA_CP_FILE=%JAVA_CP_FILE:/=\%.class

@if not exist .pack\src\%JAVA_CP_FILE% @(
   @SET JAVA_PACK_ERROR=YES
   @SET JAVA_CONT=
   @echo %JAVA_CP_FILE% not found
   goto :EOF
)

@call :MAKE_PATH_FOR_FILE .pack\dest\%JAVA_CP_FILE%
@copy/y .pack\src\%JAVA_CP_FILE% .pack\dest\%JAVA_CP_FILE%
@echo %JAVA_CP_FILE%
@goto :EOF

:MAKE_PATH_FOR_FILE
@if not exist %~dp1 @(
  @mkdir %~dp1
  @echo mkdir %~dp1
)
@goto :EOF

:JAVA_MAKE

    echo env setting
  
    SET JAVA_OUTPUT=%CD%
  
    if exist jmake_cfg.bat (
        call jmake_cfg.bat
    )

    if not exist src cd ..

    if not defined JAVA_LIB    SET JAVA_LIB=%CD%\lib
    if not defined JAVA_SOURCE SET JAVA_SOURCE=%CD%\src
    SET JAVA_CLASSES=%CD%\classes
    if not defined JAVA_PROJECT_NAME call :SET_JAVA_PROJECT_NAME %CD%

    call :CREATE_CLASSES

echo begin compile

  pushd %JAVA_SOURCE%

  for /R %%i in (*.java) do (
      echo compiling %%~nxi
      "%JAVA_HOME%\bin\javac" -classpath %JAVA_CLASSES%;%JAVA_SOURCE% -d %JAVA_CLASSES% %%i
  )

  popd

echo end compile


echo begin make jar

  call :COPY_DATA_FILES

  pushd %JAVA_CLASSES%

  jar cvfm %JAVA_OUTPUT%\%JAVA_PROJECT_NAME%.jar %JAVA_OUTPUT%\MANIFEST.MF *

  popd

echo end make jar

echo clean up

  popd

  if exist %JAVA_CLASSES% rd/s/q %JAVA_CLASSES%

  SET JAVA_CLASSES=
  SET JAVA_LIB=
  SET JAVA_OUTPUT=
  SET JAVA_SOURCE=

echo make finished

goto :EOF


:CREATE_CLASSES

  echo begin create classess

  if exist %JAVA_CLASSES% rd/s/q %JAVA_CLASSES%
  MKDIR %JAVA_CLASSES%

  if not exist %JAVA_LIB% goto :CREATE_CLASSES_END

  pushd %JAVA_CLASSES%

  for /F %%i in ('dir /B %JAVA_LIB%\*.jar') do (
    echo extract lib [%%i] to classes
    jar xf %JAVA_LIB%\%%i
  )

  popd

  if exist %JAVA_CLASSES%\META-INF rd/s/q %JAVA_CLASSES%\META-INF

:CREATE_CLASSES_END

  echo end create classes

goto :EOF

:COPY_DATA_FILES
  if not defined JAVA_DATA_FILES goto :EOF

  for %%i in (%JAVA_DATA_FILES%) do (
    copy /y %JAVA_SOURCE%\%%i %JAVA_CLASSES%\%%i
  )

goto :EOF


:DELETE_IF_SIZE_EQ_0
  SET FILE_SIZE=%~z1
  if %FILE_SIZE% EQU 0 del/s/q %1
  SET FILE_SIZE=
goto :EOF

:SET_JAVA_PROJECT_NAME
  SET JAVA_PROJECT_NAME=%~n1
goto :EOF